
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using TitanX;

using System;

[Serializable]
public class Pool {
    //对象池名称
   public string poolName;
    //预制体
    public GameObject prefab;

    //初始化大小
    public int initNum;

    public List<GameObject> list;


       
  //初始化对象池
    public void initPool(){
        
     
        for(int i=0;i<initNum;i++){
           // GameObject gmpre= Instantiate(prefab);
            GameObject gmpre=new GameObject();
            gmpre.name=poolName+"("+"i"+")";
            gmpre.SetActive(false);
            list.Add(gmpre);
        }

    }






//取出当前列表中可以的对象  不存在就新建一个然后返回
    public GameObject availableObject(){

        GameObject availableObject = new GameObject(); 

        foreach(GameObject gm in list){
            if(!gm.active){
               availableObject= gm;
            }

        }


        if(availableObject==null){
            //不存在就生成一个
            //availableObject=Instantiate(prefab);
            availableObject=new GameObject();
            availableObject.SetActive(false);
            list.Add(availableObject);
        }


        return availableObject;
    }





    //生成一个对象
    public GameObject preparObject(){

   
        GameObject availableObject = this.availableObject();
            
        availableObject.SetActive(true);
        return availableObject;

    }




    //删除指定的元素
    public void deleteObject(GameObject gm){

        for(int i=0;i<list.Count;i++){
                    if(list[i]==gm){

                        gm.SetActive(false);

                    }


            }
    }

    //清空整个对象池
       public void deleteAllObject(){

        for(int i=0;i<list.Count;i++){
            list[i].SetActive(false);
            }
    }


}






public  class PoolManger:SingletonGetMono<PoolManger>{

    public  ObjectPool[] pools;


    public Dictionary<string,ObjectPool> objectPoolDic=new Dictionary<string, ObjectPool>();



    private void Start()
    {
            objectPoolDic.Add("emeny",pools[0]);
             objectPoolDic.Add("pitfall",pools[1]);
             objectPoolDic.Add("effect", pools[2]);
         InitAllPools();
    }

    public void  InitAllPools(){

        foreach(ObjectPool pool in pools){
            pool.initPool();
        }

    }


    //从对象池取出对象中
    public GameObject OutPool(string poolName){

       //再生成每个对象之前，先

      return  objectPoolDic[poolName].preparObject();


    }



    //将对象放回对象池中
    public void EnterPool(string poolName,GameObject gm){

        objectPoolDic[poolName].deleteObject(gm);
    }



    //清空对象池
    public void clearPool(string poolName)
    {
        objectPoolDic[poolName].deleteAllObject();

    }


    public List<GameObject> getAllSavePool(string poolName) {

        return objectPoolDic[poolName].getAllSavePool();

    }



    




}







